﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADC.DAL;

namespace ADC.BL
{
    public class UserFactory
    {
        public static User CheckLogin(User user)
        {
            try
            {
                var db = new dbADCEntities();
                user.Password = ADC.Utilites.Common.EncodePassword(user.Password);

                var userInfo = db.Users.Where(g => g.UserName == user.UserName).Where(g => g.Password == user.Password).FirstOrDefault();

                return userInfo;
            }
            catch (Exception)
            {
                // TODO: log here  
                throw;
            }
            
        }

        public static User GetUserByName(String userName)
        {
          var db = new dbADCEntities();

          var userInfo = db.Users.Where(g => g.UserName == userName).FirstOrDefault();
          return userInfo;

        }

        public static int ChangePassword(User user, string newPassword)
        {
            var db = new dbADCEntities();
            var userInfo = db.Users.Where(g => g.UserName == user.UserName).FirstOrDefault();

            if (userInfo == null) return -1;

            userInfo.Password = ADC.Utilites.Common.EncodePassword(newPassword);
            return db.SaveChanges();
        }


        public static List<User> GetAll()
        {
          var db = new dbADCEntities();
          return db.Users.ToList();
        }

        public static int Delete(int userID)
        {
          var db = new dbADCEntities();
          var user = db.Users.Where(g => g.UserID.Equals(userID)).FirstOrDefault();
          db.DeleteObject(user);
          return db.SaveChanges();
        }

        public static int Update(User user, int userTypeID)
        {
          var db = new dbADCEntities();
          var objUser = db.Users.Where(g => g.UserID.Equals(user.UserID)).FirstOrDefault();
          var roleType = db.Roles.Where(g => g.RoleID == userTypeID).FirstOrDefault();
          objUser.Role = roleType;
          objUser.UserName = user.UserName;
          objUser.Name = user.Name;
          objUser.Password = user.Password;
          objUser.Address = user.Address;
          objUser.Email = user.Email;
          objUser.ModifiedDate = DateTime.Now;
          return db.SaveChanges();
        }

        public static User GetByID(int userID)
        {
          var db = new dbADCEntities();
          return db.Users.Where(g => g.UserID == userID).FirstOrDefault();
        }

        public static int Insert(User user, int roleID)
        {
          var db = new dbADCEntities();
          var role = db.Roles.Where(g => g.RoleID == roleID).FirstOrDefault();
          user.Role = role;
          db.AddToUsers(user);
          return db.SaveChanges();
        }

        public static List<Role> GetlAllRole()
        {
          var db = new dbADCEntities();
          return db.Roles.ToList();
        }

    }
}
